package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    //新增套餐
    @Transactional
    @Override
    public void saveSetMeal(SetmealDTO setmealDTO) {
        //1.将套餐的基本信息进行插入
        Setmeal setMeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setMeal);
        setmealMapper.insert(setMeal);
        //2.获取套餐id
        Long setmealId = setMeal.getId();
        //3.将套餐中的菜品信息进行插入,一个套餐中包含多个菜品,进行批量插入,先给所有的套餐-菜品赋值
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealId);
        }
        log.info("setmealDishList:{}",setmealDishList);
        setmealDishMapper.insertBatch(setmealDishList);
    }

    //套餐分页查询
    @Override
    public PageResult queryByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        int page = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        //使用pageHelper插件实现分页查询
        PageHelper.startPage(page,pageSize);
        //返回视图的数据使用SetmealVO进行封装
        List<SetmealVO> setmealVOList = setmealMapper.queryByPage(setmealPageQueryDTO);
        //使用PageInfo对结果进行封装
        PageInfo<SetmealVO> pageInfo = new PageInfo<>(setmealVOList);
        //将获得结果进行封装
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getList());
        //将产生PageResult并进行返回
        return pageResult;
    }
    //套餐起售，停售
    @Transactional
    @Override
    public void startOrStop(Integer status, Long id) {
        //当起售的时候，如果里面的商品是停售的状态则无法起售，
        //获取该套餐中的所包含的菜品的实例，然后进行判断
        List<Dish> dishList = dishMapper.queryBySetMealId(id);
        for (Dish dish : dishList) {
            if(dish.getStatus() == StatusConstant.DISABLE){
                throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        //修改套餐的状态
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.update(setmeal);
    }

    //根据id进行查询套餐
    @Transactional
    @Override
    public SetmealVO queryById(Long id) {
        //1.根据id获取套餐的基本信息
        Setmeal setmeal = setmealMapper.queryById(id);
        //2.根据套餐id获取套餐-菜品的基本信息
        List<SetmealDish> setmealDishList = setmealDishMapper.queryById(id);
        //3.将获取到的数据进行封装
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }
    //批量删除套餐
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //根据id查询套餐，返回套餐，然后进行比对，看是否是起售状态，如果是起售状态则进行抛出异常
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.queryById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //批量删除套餐的基本信息
        setmealMapper.deleteByIds(ids);
        //批量删除套餐-菜品的关联信息
        setmealDishMapper.deleteByIds(ids);
    }

    //根据菜品分类的ID进行查询套餐
    @Override
    public List<Setmeal> queryByCategoryId(Long categoryId) {
        List<Setmeal> setmealList = setmealMapper.queryByCategoryId(categoryId);
        return setmealList;
    }

    //根据套餐的id查询菜品
    @Override
    public List<DishItemVO> queryDishBySetMealId(Long setmealId) {
        List<DishItemVO> dishItemVOList = setmealDishMapper.queryDishBySetMealId(setmealId);
        return dishItemVOList;
    }

    //修改套餐
    @Transactional
    @Override
    public void updateById(SetmealDTO setmealDTO) {
        //创建套餐的实体对象进行修改套餐的基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //调用setmealMapper进行数据库的更新操作
        setmealMapper.update(setmeal);
        //获取套餐的id
        long setmealId = setmealDTO.getId();
        //根据套餐id进行套餐-菜品批量删除数据
        setmealDishMapper.deleteById(setmealId);
        //获取所有的套餐-菜品的信息
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        //进行批量的插入操作
        setmealDishMapper.insertBatch(setmealDishList);
    }
}
